<?php
class Alipay
{
	//public $m_partnerid = '2088121909567903';//Partner
	public $m_partnerid = '2088621796126903';//Partner
	//public $m_partnerkey = 'ef190qpllefsb867wv60xuvot91oabvz';//秘钥
	public $m_partnerkey = 'hxqv33wtjv8fu2ru738hbhcoxe5bykd8';//秘钥
	public $m_partnerid_pay = ''; //支付账号id
	public $charset = 'utf-8';
	public $signtype = 'MD5';
	public $url = 'https://mapi.alipay.com/gateway.do?';
    public $return_url = '';
	public function __construct(){
        
    }
	
	
	//查询交易
	public function queryone($orderid,$out_trade_no=null){
		$data['service'] = 'single_trade_query';
		$data['partner'] = $this->m_partnerid;
		if($out_trade_no){
			$data['out_trade_no'] = $out_trade_no;
		}else{
			$data['trade_no'] = $orderid;
		}
		$data['sign'] = $this->sign($data);
		$data['sign_type'] = $this->signtype;
		$data['_input_charset'] = $this->charset;
		$params = array();
		foreach ($data as $k=>$v){
			$params[] = $k.'='.urlencode($v);
		}
		$param = implode('&', $params);
		$url = $this->url.$param;
		$result = $this->postparam($url);
		return $result;
	}
	
	//发送数据
	public function postparam($url){
		$ch = curl_init($url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$content = curl_exec($ch);
		curl_close($ch);
		$xml=simplexml_load_string($content);
		return $xml;
	}
	
	
	//获取签名字符串
    public function sign($data){
    	$value = array();
    	$value[] = '_input_charset='.$this->charset;
    	foreach ($data as $k=>$v){
    		$value[] = $k.'='.$v;
    	}
    	asort($value);
    	$valuestr = implode('&', $value);
    	$sign = $valuestr.$this->m_partnerkey;
    	return md5($sign);
    }


}